package com.augn.service.impl;

import com.augn.entity.Goods;
import com.augn.entity.Manufacturer;
import com.augn.entity.Result;
import com.augn.entity.Style;
import com.augn.mapper.ManufacturerMapper;
import com.augn.service.IManufacturerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author augn
 * @since 2022-09-22
 */
@Service
public class ManufacturerServiceImpl extends ServiceImpl<ManufacturerMapper, Manufacturer> implements IManufacturerService {

    @Autowired
    private ManufacturerMapper mapper;

    //新增一个制造商
    @Override
    public Result<?> insertOneManufacturer(Manufacturer manufacturer) {
        int insert = mapper.insert(manufacturer);
        if(insert==0){
            return Result.error("-1","新增失败");
        } else{
            return Result.success(insert);
        }
    }

    //删除一个制造商
    @Override
    public Result<?> deleteOneManufacturer(Integer id) {
        int delete = mapper.deleteById(id);
        if(delete==0){
            return Result.error("-1","删除失败");
        } else{
            return Result.success(delete);
        }
    }

    //更新一个制造商
    @Override
    public Result<?> updateOneManufacturer(Manufacturer manufacturer) {
        int update = mapper.updateById(manufacturer);
        if(update==0){
            return Result.error("-1","更新失败");
        } else{
            return Result.success(update);
        }
    }

    // 获取一个制造商数组
    @Override
    public Result<?> getPartManufacturer(Manufacturer manufacturer,Integer pageNum,Integer pageSize) {
        QueryWrapper<Manufacturer> wrapper = new QueryWrapper<>();
        if (manufacturer.getManuId() != null) {
            wrapper.eq("manu_id", manufacturer.getManuId());
        } else {
            wrapper.like("name", manufacturer.getName());
        }

        long pages ,total;
        List<Manufacturer> selectList ;
        Map<String,Object> ret = new HashMap<>();
        if((pageNum==null&&pageSize==null)||(pageNum==null&&pageSize!=null)){
            selectList =  mapper.selectList(wrapper);
            ret.put("selectList",selectList);
        } else {
            if(pageSize==null){
                pageSize=20;
            }
            Page<Manufacturer> page = new Page<>(pageNum , pageSize);
            IPage<Manufacturer> iPage = mapper.selectPage(page , wrapper);
            selectList = iPage.getRecords();
            pages = iPage.getPages();
            total = iPage.getTotal();
            ret.put("goodsList",selectList);
            ret.put("pages",pages);
            ret.put("total",total);
        }

        if (selectList.isEmpty()) {
            return Result.error("-1", "无符合此条件的制造商");
        } else {
            return Result.success(ret);
        }
    }
}
